/*
 * CacheParametros2.java
 *
 * Created on January 11, 2008, 1:27 AM
 */

package br.upe.dsc.caeto.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;

import javax.swing.JOptionPane;

import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;

import br.upe.dsc.caeto.core.ElementNotFoundException;
import br.upe.dsc.caeto.core.InvalidElementException;
import br.upe.dsc.caeto.core.cacheunit.CacheUnit;
import br.upe.dsc.caeto.core.cacheunit.CacheUnitExplorer;
import br.upe.dsc.caeto.core.cacheunit.CacheUnitSimple;
import br.upe.dsc.caeto.core.hierarchy.Hierarchy;

public class CacheParametersDialog extends javax.swing.JDialog {
	private String cacheName;
	private Hierarchy hierarchy;
	
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel cacheUnitTypeLabel;
    private javax.swing.JComboBox cacheUnitComboBox;
    
    /*Panel Explorer*/
    private javax.swing.JComboBox minAssociativityComboBox;
    private javax.swing.JComboBox maxAssociativityComboBox;
    private javax.swing.JComboBox minLineSizeComboBox;
    private javax.swing.JComboBox maxLineSizeComboBox;
    private javax.swing.JComboBox minCacheSizeComboBox;
    private javax.swing.JComboBox maxCacheSizeComboBox;
    private javax.swing.JLabel minAssociativityLabel;
    private javax.swing.JLabel maxAssociativityLabel;
    private javax.swing.JLabel minLineSizeLabel;
    private javax.swing.JLabel maxLineSizeLabel;
    private javax.swing.JLabel minCacheSizeLabel;
    private javax.swing.JLabel maxCacheSizeLabel;
    private javax.swing.JPanel associativityPanel;
    private javax.swing.JPanel lineSizePanel;
    private javax.swing.JPanel cacheSizePanel;
    private javax.swing.JPanel explorerPanel;
    javax.swing.GroupLayout layoutExplorer;
    
    /*Panel Simple*/
    private javax.swing.JComboBox associativityComboBox;
    private javax.swing.JComboBox lineSizeComboBox;
    private javax.swing.JComboBox cacheSizeComboBox;
    private javax.swing.JLabel associativityLabel;
    private javax.swing.JLabel lineSizeLabel;
    private javax.swing.JLabel cacheSizeLabel;
    private javax.swing.JPanel simplePanel;
    javax.swing.GroupLayout layoutSimple;
	
    public CacheParametersDialog(java.awt.Frame parent, String cacheName,
    		Hierarchy hierarchy) {
        super(parent, true);
        this.cacheName = cacheName;
        this.hierarchy = hierarchy;
        initComponents();
        initDialog();
    }
    
    private void initComponents() {
        cacheUnitTypeLabel = new javax.swing.JLabel();
        cacheUnitComboBox = new javax.swing.JComboBox();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(cacheName + " - Cache Parameters");
        setAlwaysOnTop(true);
        setResizable(false);
        
        ResourceMap resource = Application.getInstance(CaetoApplication.class)
        	.getContext().getResourceManager()
        		.getResourceMap(CacheParametersDialog.class);
        
        cacheUnitTypeLabel.setName("cacheUnitTypeLabel");
        cacheUnitComboBox.setModel(new javax.swing.DefaultComboBoxModel(
        		new String[] { "Simple", "Explorer" }));

        okButton.setName("okButton");
        okButton.setPreferredSize(new java.awt.Dimension(75, 29));

        cancelButton.setName("cancelButton");
        cancelButton.setPreferredSize(new java.awt.Dimension(75, 29));
    	
    	/*Simple Panel*/
        simplePanel = new javax.swing.JPanel();
        associativityLabel = new javax.swing.JLabel();
        associativityComboBox = new javax.swing.JComboBox();
        lineSizeLabel = new javax.swing.JLabel();
        lineSizeComboBox = new javax.swing.JComboBox();
        cacheSizeLabel = new javax.swing.JLabel();
        cacheSizeComboBox = new javax.swing.JComboBox();

        associativityLabel.setName("associativityLabel");
		for (int i = 1; i < 33; i++) {
			associativityComboBox.addItem(i);
		}
		
		lineSizeLabel.setName("lineSizeLabel");
		for (int i = 4; i < 10; i++) {
			lineSizeComboBox.addItem((int) Math.pow(2,i) + " - Bytes");
		}
		
		cacheSizeLabel.setName("cacheSizeLabel");
		for (int i = 6; i < 10; i++) {
			cacheSizeComboBox.addItem((long) Math.pow(2,i) + " - Bytes");
		}
		for (int i = 0; i < 10; i++) {
			cacheSizeComboBox.addItem((long) Math.pow(2,i) + " - KB");
		}
		for (int i = 0; i < 10; i++) {
			cacheSizeComboBox.addItem((long) Math.pow(2,i) + " - MB");
		}

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(simplePanel);
        simplePanel.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(associativityLabel)
                    .addComponent(lineSizeLabel)
                    .addComponent(cacheSizeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lineSizeComboBox, 0, 214, Short.MAX_VALUE)
                    .addComponent(associativityComboBox, 0, 214, Short.MAX_VALUE)
                    .addComponent(cacheSizeComboBox, 0, 214, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(associativityLabel)
                    .addComponent(associativityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lineSizeLabel)
                    .addComponent(lineSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cacheSizeLabel)
                    .addComponent(cacheSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layoutSimple = new javax.swing.GroupLayout(getContentPane());
        layoutSimple.setHorizontalGroup(
            layoutSimple.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layoutSimple.createSequentialGroup()
                .addContainerGap()
                .addComponent(cacheUnitTypeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cacheUnitComboBox, 0, 195, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(simplePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layoutSimple.createSequentialGroup()
                .addContainerGap(153, Short.MAX_VALUE)
                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layoutSimple.setVerticalGroup(
            layoutSimple.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layoutSimple.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layoutSimple.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cacheUnitTypeLabel)
                    .addComponent(cacheUnitComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(simplePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layoutSimple.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );
    	
    	/*Explorer Panel*/
        explorerPanel = new javax.swing.JPanel();
        explorerPanel.setVisible(false);
        associativityPanel = new javax.swing.JPanel();
        minAssociativityLabel = new javax.swing.JLabel();
        maxAssociativityLabel = new javax.swing.JLabel();
        minAssociativityComboBox = new javax.swing.JComboBox();
        maxAssociativityComboBox = new javax.swing.JComboBox();
        lineSizePanel = new javax.swing.JPanel();
        minLineSizeLabel = new javax.swing.JLabel();
        maxLineSizeLabel = new javax.swing.JLabel();
        minLineSizeComboBox = new javax.swing.JComboBox();
        maxLineSizeComboBox = new javax.swing.JComboBox();
        cacheSizePanel = new javax.swing.JPanel();
        minCacheSizeLabel = new javax.swing.JLabel();
        maxCacheSizeLabel = new javax.swing.JLabel();
        minCacheSizeComboBox = new javax.swing.JComboBox();
        maxCacheSizeComboBox = new javax.swing.JComboBox();

        associativityPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Associativity:"));

        minAssociativityLabel.setName("minAssociativityLabel");
        maxAssociativityLabel.setName("maxAssociativityLabel");

		for (int i = 1; i < 33; i++) {
			minAssociativityComboBox.addItem(i);
			maxAssociativityComboBox.addItem(i);
		}

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(associativityPanel);
        associativityPanel.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(minAssociativityLabel)
                    .addComponent(maxAssociativityLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(minAssociativityComboBox, 0, 220, Short.MAX_VALUE)
                    .addComponent(maxAssociativityComboBox, 0, 220, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(minAssociativityLabel)
                    .addComponent(minAssociativityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxAssociativityLabel)
                    .addComponent(maxAssociativityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lineSizePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Line Size:"));

        minLineSizeLabel.setName("minLineSizeLabel");
        maxLineSizeLabel.setName("maxLineSizeLabel");

		for (int i = 6; i < 10; i++) {
			minLineSizeComboBox.addItem((int) Math.pow(2,i) + " - Bytes");
			maxLineSizeComboBox.addItem((int) Math.pow(2,i) + " - Bytes");
		}

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(lineSizePanel);
        lineSizePanel.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(minLineSizeLabel)
                    .addComponent(maxLineSizeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(minLineSizeComboBox, 0, 220, Short.MAX_VALUE)
                    .addComponent(maxLineSizeComboBox, 0, 220, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(minLineSizeLabel)
                    .addComponent(minLineSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxLineSizeLabel)
                    .addComponent(maxLineSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        cacheSizePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Cache Size:"));

        minCacheSizeLabel.setName("minCacheSizeLabel");
        maxCacheSizeLabel.setName("maxCacheSizeLabel");
        
		for (int i = 6; i < 10; i++) {
			minCacheSizeComboBox.addItem((long) Math.pow(2,i) + " - Bytes");
			maxCacheSizeComboBox.addItem((long) Math.pow(2,i) + " - Bytes");
		}
		for (int i = 0; i < 10; i++) {
			minCacheSizeComboBox.addItem((long) Math.pow(2,i) + " - KB");
			maxCacheSizeComboBox.addItem((long) Math.pow(2,i) + " - KB");
		}
		for (int i = 0; i < 10; i++) {
			minCacheSizeComboBox.addItem((long) Math.pow(2,i) + " - MB");
			maxCacheSizeComboBox.addItem((long) Math.pow(2,i) + " - MB");
		}

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(cacheSizePanel);
        cacheSizePanel.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(minCacheSizeLabel)
                    .addComponent(maxCacheSizeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(minCacheSizeComboBox, 0, 220, Short.MAX_VALUE)
                    .addComponent(maxCacheSizeComboBox, 0, 220, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(minCacheSizeLabel)
                    .addComponent(minCacheSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxCacheSizeLabel)
                    .addComponent(maxCacheSizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(explorerPanel);
        explorerPanel.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(associativityPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(lineSizePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(cacheSizePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(associativityPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lineSizePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cacheSizePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layoutExplorer = new javax.swing.GroupLayout(getContentPane());
        layoutExplorer.setHorizontalGroup(
            layoutExplorer.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layoutExplorer.createSequentialGroup()
                .addContainerGap()
                .addGroup(layoutExplorer.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layoutExplorer.createSequentialGroup()
                        .addComponent(cacheUnitTypeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cacheUnitComboBox, 0, 195, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layoutExplorer.createSequentialGroup()
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addComponent(explorerPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layoutExplorer.setVerticalGroup(
            layoutExplorer.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layoutExplorer.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layoutExplorer.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cacheUnitTypeLabel)
                    .addComponent(cacheUnitComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(explorerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layoutExplorer.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        resource.injectComponents(this);
        
        cacheUnitComboBox.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		changeCacheUnitType(cacheUnitComboBox.getSelectedIndex());
        	}
        });
        
        okButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		addCacheUnit();
        	}
        });
        
        cancelButton.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		dispose();
        	}
        });
        
        pack();
    }
    
	private void initDialog() {
		CacheUnit cache;
		CacheUnitSimple simple;
		CacheUnitExplorer explorer;
		int cacheUnitType = 1;
		
		try {
			if (hierarchy.getCacheUnits().isElement(cacheName)) {
				System.out.println(cacheName);
				cache = hierarchy.getCacheUnits().search(cacheName);
				if (cache instanceof CacheUnitSimple) {
					cacheUnitComboBox.setSelectedIndex(0);
					cacheUnitType = 0;
					changeCacheUnitType(cacheUnitType);
					
					simple = (CacheUnitSimple) cache;
					associativityComboBox.setSelectedItem(simple.getAssociatividade());
					lineSizeComboBox.setSelectedItem(
							toComboBox(simple.getTamLinha()));
					cacheSizeComboBox.setSelectedItem(
							toComboBox(simple.getTamanho()));
					
					
				} else {
					cacheUnitComboBox.setSelectedIndex(1);
					cacheUnitType = 1;
					changeCacheUnitType(cacheUnitType);
					
					explorer = (CacheUnitExplorer) cache;
					minAssociativityComboBox.setSelectedItem(
							explorer.getAssociatividade()[0]);
					maxAssociativityComboBox.setSelectedItem(
							explorer.getAssociatividade()[1]);
					minLineSizeComboBox.setSelectedItem(
							toComboBox(explorer.getTamLinha()[0]));
					maxLineSizeComboBox.setSelectedItem(
							toComboBox(explorer.getTamLinha()[1]));
					minCacheSizeComboBox.setSelectedItem(
							toComboBox(explorer.getTamanho()[0]));
					maxCacheSizeComboBox.setSelectedItem(
							toComboBox(explorer.getTamanho()[1]));
					
				}
			}
		}
		catch (ElementNotFoundException e) {
			System.out.println(e.getMessage());
		}
	}
    
    private void addCacheUnit() {
    	CacheUnit cache = null;

    	int associativity[] = new int[2];
		int lineSize[] = new int[2];
		int cacheSize[] = new int[2];
    		
    	if (cacheUnitComboBox.getSelectedIndex() == 0) {
    		/*CacheUnitSimple*/
    		associativity[0] = (Integer) associativityComboBox.getSelectedItem();
    		lineSize[0] = toByte((String) lineSizeComboBox.getSelectedItem());
    		cacheSize[0] = toByte((String) cacheSizeComboBox.getSelectedItem());
    		
    		cache = new CacheUnitSimple(cacheName,
    				associativity[0], lineSize[0], cacheSize[0]);
    	} else {
    		/*CacheUnitExplorer*/
    		associativity[0] = (Integer) minAssociativityComboBox.getSelectedItem();
    		associativity[1] = (Integer) maxAssociativityComboBox.getSelectedItem();
    		lineSize[0] = toByte((String) minLineSizeComboBox.getSelectedItem());
    		lineSize[1] = toByte((String) maxLineSizeComboBox.getSelectedItem());
    		cacheSize[0] = toByte((String) minCacheSizeComboBox.getSelectedItem());
    		cacheSize[1] = toByte((String) maxCacheSizeComboBox.getSelectedItem());
    		
    		cache = new CacheUnitExplorer(cacheName,
    				associativity, lineSize, cacheSize);
    	}
    	try {
			if (!hierarchy.getCacheUnits().isElement(cacheName)) {
				hierarchy.addCacheUnit(cache);
			}
			else {
				hierarchy.getCacheUnits().update(cache);
			}
			this.dispose();
		}
		catch (InvalidElementException e) {
			JOptionPane.showMessageDialog(this, e.getMessage(),
					"Error", JOptionPane.ERROR_MESSAGE);
		}
    }
    
    private void changeCacheUnitType(int cacheUnitType) {
		if (cacheUnitType == 0) {
			explorerPanel.setVisible(false);
			simplePanel.setVisible(true);
			getContentPane().setLayout(layoutSimple);
			setSize(347, 252);
		}
		else {
			simplePanel.setVisible(false);
			explorerPanel.setVisible(true);
			getContentPane().setLayout(layoutExplorer);
			setSize(347, 447);
		}
		
		pack();
    }
    
    private int toByte(String converter) {
		StringTokenizer token = new StringTokenizer(converter," - ");
		int valor = Integer.parseInt(token.nextToken());
		String unidade = token.nextToken();
		
		if (unidade.equals("Bytes")) {
			return valor;
		}
		else if (unidade.equals("KB")) {
			return (1024*valor);
		}
		else { //MB
			return (1048576*valor); //1048576 == 1024*1024 == 2 ^ 20
		}	
	}

	private String toComboBox(int valor) {
		if (valor == 1048576) {
			return "1 - MB";
		}
		else if (valor > 1048576) {
			return (valor/1048576 + " - MB");
		}
		else if (valor == 1024) {
			return "1 - KB";
		}
		else if (valor > 1024) {
			return (valor/1024 + " - KB");
		}
		else {
			return (valor + " - Bytes");
		}
	}
}
